<template>
  <div class="mod-whitelist">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="searchForm.type" clearable filterable placeholder="活动名称">
          <el-option
            v-for="type in whitelistType"
            :key="type.value"
            :label="type.label"
            :value="type.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.phone" clearable placeholder="手机号码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.idCard" clearable placeholder="身份证后6位"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('mall:whitelist:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('mall:whitelist:save')" type="primary" @click="dataImport()">导入</el-button>
        <el-button v-if="isAuth('mall:whitelist:delete')" :disabled="dataListSelections.length <= 0" type="danger"
                   @click="deleteHandle()">批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle">
      <el-table-column
        align="center"
        header-align="center"
        type="selection"
        width="50">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="姓名"
        prop="name">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="身份证号"
        prop="idCard">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="手机"
        prop="phone">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="卡号"
        prop="acctNo">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="新增 时间"
        prop="addTime">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="修改时间"
        prop="modifedTime">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="状态"
        prop="valid">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.valid === 0" size="small" type="danger">无效</el-tag>
          <el-tag v-else-if="scope.row.valid === 1" size="small" type="success">有效</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="right"
        header-align="center"
        label="操作"
        width="150">
        <template slot-scope="scope">
          <el-button v-if="isAuth('mall:whitelist:info')" size="small" type="text" @click="showDetails(scope.row.id)">
            查看
          </el-button>
          <el-button v-if="isAuth('mall:whitelist:update')" size="small" type="text"
                     @click="addOrUpdateHandle(scope.row.id)">修改
          </el-button>
          <el-button v-if="isAuth('mall:whitelist:delete')" size="small" type="text"
                     @click="deleteHandle(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

    <data-import ref="dataImport" @refreshDataList="getDataList"></data-import>

  </div>
</template>

<script>
import AddOrUpdate from './whitelist-add-or-update'

export default {
  data() {
    return {
      searchForm: {
        phone: '',
        type: '',
        idCard: ''
      },
      whitelistType: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListSelections: [],
      addOrUpdateVisible: false,
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
    this.$http({
      url: '/sys/dict/queryByCode',
      method: 'get',
      params: {
        'code': 'WHITELIST_TYPE'
      }
    }).then(({data}) => {
      if (data && data.code === 0) {
        this.whitelistType = data.list.map(x => {
          return {label: x.name, value: x.value}
        })
      } else {
        this.whitelistType = []
      }
    })
  },
  methods: {
    // 获取数据列表
    getDataList() {
      let params = JSON.parse(JSON.stringify(this.searchForm))
      params['page'] = this.pageIndex
      params['limit'] = this.pageSize
      this.$http({
        url: '/mall/whitelist/list',
        method: 'get',
        params
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 查看详情
    showDetails(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, true, this.whitelistType)
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, false, this.whitelistType)
      })
    },
    // 数据导入
    dataImport() {
      // 模板配置
      let modelInfo = {
        default: {
          titles: ['姓名', '身份证号(后6位)', '手机号码','次数','等级'],
          fileName: '白名单导入模板',
          data: [[]]
        },
        1: {
          titles: ['姓名', '身份证号(后6位)', '手机号码', '次数','等级'],
          fileName: '白名单导入模板',
          data: [[]]
        }
      }
      // 导入
      this.$refs.dataImport.init('/mall/whitelist/upload', modelInfo, this.whitelistType,true)
    },
    // 删除
    deleteHandle(id) {
      let ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '/mall/whitelist/delete',
          method: 'post',
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getDataList()
          }
        })
      }).catch(() => {
      })
    },
  }
}
</script>
